<?php
/**
 * Created by PhpStorm.
 * User: mabao
 * 专门用来跑定时器的
 */
namespace app\api\controller;
use think\Db;
use think\Config;
class Timer extends Base
{
    /**
     * 每分钟执行一次：首页刷新置顶包（top表和house表：改置顶过了没）
     */
    public function minuteTimer()
    {
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $minute_3 = date("Y-m-d H:i:s",(strtotime("{$today}")-60*3));
        $minute_30 = date("Y-m-d H:i:s",(strtotime("{$today}")-60*30));
        //1、先改top表的精选置顶和二手置顶
        $shi_ids = Db::table('up_top')//a、以市分组，查出过期的市的分布
            ->where('top_have_one',1)
            ->where('top_updatetime','<=',$minute_3)
            ->group('top_shi')
            ->order('top_time ASC')
            ->column('top_shi');
        if($shi_ids){
            foreach ($shi_ids as $k=>$v){
                $topids_5 = Db::table('up_top')//b、此市前5
                    ->where('top_have_one',1)
                    ->where('top_shi',$v)
                    ->order('top_time ASC')
                    ->limit(0,5)
                    ->column('top_id');
                Db::table('up_top')//c、此市的在这5个里的过期的改为展示过
                    ->where('top_have_one',1)
                    ->where('top_updatetime','<=',$minute_3)
                    ->where('top_shi',$v)
                    ->where('top_id','in',$topids_5)
                    ->update(['top_have_one'=>2]);
                Db::table('up_top')//d、此市的不在这5个里的没展示过的改判断时间
                    ->where('top_have_one',1)
                    ->where('top_shi',$v)
                    ->where('top_id','not in',$topids_5)
                    ->update(['top_updatetime'=>$today]);
            }
        }
        $shi_ids2 = Db::table('up_top')
            ->where('top_type',2)
            ->where('top_have_two',1)
            ->where('top_updatetime2','<=',$minute_30)
            ->group('top_shi')
            ->order('top_time ASC')
            ->column('top_shi');
        if($shi_ids2){
            foreach ($shi_ids2 as $k=>$v){
                $topids_5 = Db::table('up_top')
                    ->where('top_type',2)
                    ->where('top_have_two',1)
                    ->where('top_shi',$v)
                    ->order('top_time ASC')
                    ->limit(0,5)
                    ->column('top_id');
                Db::table('up_top')
                    ->where('top_type',2)
                    ->where('top_have_two',1)
                    ->where('top_updatetime2','<=',$minute_30)
                    ->where('top_shi',$v)
                    ->where('top_id','in',$topids_5)
                    ->update(['top_have_two'=>2]);
                Db::table('up_top')
                    ->where('top_type',2)
                    ->where('top_have_two',1)
                    ->where('top_shi',$v)
                    ->where('top_id','not in',$topids_5)
                    ->update(['top_updatetime2'=>$today]);
            }
        }
        //2、再改house表的置顶
        $shi_ids = Db::table('up_house')
            ->where('h_have_top',1)
            ->where('h_updatetime','<=',$minute_3)
            ->group('h_shi')
            ->order('h_time ASC')
            ->column('h_shi');
        if($shi_ids){
            foreach ($shi_ids as $k=>$v){
                $topids_5 = Db::table('up_house')
                    ->where('h_have_top',1)
                    ->where('h_shi',$v)
                    ->order('h_time ASC')
                    ->limit(0,5)
                    ->column('h_id');
                Db::table('up_house')
                    ->where('h_have_top',1)
                    ->where('h_updatetime','<=',$minute_3)
                    ->where('h_shi',$v)
                    ->where('h_id','in',$topids_5)
                    ->update(['h_have_top'=>2]);
                Db::table('up_house')
                    ->where('h_have_top',1)
                    ->where('h_shi',$v)
                    ->where('h_id','not in',$topids_5)
                    ->update(['h_updatetime'=>$today]);
            }
        }
        return true;
    }
    /**
     * 每天执行一次：下架房子和清除历史记录(以及发布次数和使用包的次数)
     */
    public function dayTimer()
    {
        set_time_limit(0);
        $today = date('Y-m-d H:i:s');
        $days_7 = date("Y-m-d H:i:s",(strtotime("{$today}")-3600*24*7));
        $days_30 = date("Y-m-d H:i:s",(strtotime("{$today}")-3600*24*30));
        //1、删除7天之前的浏览记录
        Db::table('up_history')
            ->where('his_date','<=',$days_7)
            ->delete();
        //2、30天之前的房源改为下架
        Db::table('up_house')
            ->where('h_time','<=',$days_30)
            ->update(['h_status'=>2]);
        //3、发布次数和包次数
        Db::table('up_u_user')
            ->where('user_del',1)
            ->update(['user_fabu'=>0,'user_top_use'=>0,'user_fresh_use'=>0]);
        return true;
    }

    //生成二维码
    public function ercodeForUser(){
        $users = Db::table('up_u_user')
            ->where('user_ercode','no')
            ->field('user_id')
            ->select();
        if($users){
            foreach ($users as $k => $v){
                $logo = ROOT_PATH . 'public' . DS . 'static/home/image/logo.png';
                $ercode = Qrcode("http://share.zhijiayouxuan.cn/phone.html?userid={$v['user_id']}",$logo);
                Db::table('up_u_user')->where('user_id',$v['user_id'])->setField('user_ercode',$ercode);
            }
        }
    }
    //生成二维码
    public function getUserPhone(){
        header("Access-Control-Allow-Origin: *");
        $uid = input('uid');
        $users = Db::table('up_u_user')
            ->where('user_id',$uid)
            ->value('user_phone');
        return json(['msg' => $users]);
    }
}